package com.wms.controller;

import com.interf.WCSUtils;
import com.mes.entity.User;
import com.wms.service.LastBottomPairService;
import com.yy.base.page.Message;
import com.yy.base.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("LastBottomPair")
public class LastBottomPairController {
	@Autowired
	private LastBottomPairService lastbottompairservice;

	//加载所有作业工单信息
	@ResponseBody
	@RequestMapping(value = "LastBottomPairList", method = RequestMethod.POST)
	public Message LastBottomPairList(HttpServletRequest request) {
		Message msg = new Message();

		msg.setData("JobOrderList", lastbottompairservice.getJobOrderList());

		return msg;
	}

	//根据传过来的楦头编码找到时间最近的作业工单及工单匹配的大底
	@ResponseBody
	@RequestMapping(value = "getRfidJobCode", method = RequestMethod.POST)
		public Message getRfidJobCode(HttpServletRequest request) {
			Message msg = new Message();

		String rfids = request.getParameter("rfid").toString();   //

        String rfid = rfids.replaceAll(" ", "");

		List<Map<String, Object>> manorderList = lastbottompairservice.getProductDdCode(rfid);



		if(manorderList.size() != 0){

			String WLID = manorderList.get(0).get("WLID").toString();  //物料ID

            String SIZEID = manorderList.get(0).get("SIZEID").toString();  //尺码ID

            String ZYCODE = manorderList.get(0).get("ZYCODE").toString();  //作业工单


            msg.setData("JobOrderSoles", lastbottompairservice.getBatchnoSoleLists(ZYCODE));

            msg.setData("JobOrderPair", lastbottompairservice.getJobOrderPairLists(ZYCODE));


			List<Map<String, Object>> list = lastbottompairservice.getRfidDdCode(WLID,SIZEID);
			String leftCode = null;  //左脚大底RFID
			String rightCode = null; //右脚大底RFID
			String DDRFID = null;    //合成后传递给WMS线边库的RFID
			String remarks = null;   //左右脚备注
			String leftITMEID = null;	//左脚大底ITEMID
			String rightITMEID = null;	//右脚大底ITEMID
			for (Map<String, Object> map : list) {
				if (leftCode == null) {
					DDRFID = (String)map.get("DDRFID");
					leftCode = (String)map.get("LEFTRFID");
					rightCode = (String)map.get("RIGHTRFID");
					remarks = (String)map.get("WLVALUE");
					leftITMEID = (String)map.get("LEFTITEMID");
					rightITMEID = (String)map.get("RIGHTITEMID");
					int qty = WCSUtils.check("DD", DDRFID);
					if (qty > 0) {
						//调用大底库下架
						WCSUtils.outStorck("DD", DDRFID, 1, DDRFID);
						lastbottompairservice.updateStorageBatchUpStatus(DDRFID);
					} else {
						DDRFID = null;
						leftCode = null;
						rightCode = null;
						remarks = null;
						leftITMEID = null;
						rightITMEID = null;
					}
				}
				if(DDRFID != ""){

				}
				if (DDRFID != null) {
					break;
				}
			}


			msg.setData("leftRfidJobCode", leftCode);
			msg.setData("rightRfidJobCode", rightCode);
			msg.setData("remarks", remarks);
			msg.setData("JobCodeLists", manorderList);
            msg.setData("leftITMEID", leftITMEID);
			msg.setData("rightITMEID", rightITMEID);

		}else{
			msg.setData("results", "false");
		}

		return msg;
	}

	//加载该作业订单对应的配对信息(作业工单所绑定的楦头、大底信息)
	@ResponseBody
	@RequestMapping(value = "getBottimLists", method = RequestMethod.POST)
	public Message OutConfirmLists(HttpServletRequest request) {
		Message msg = new Message();

		String JOBCODES = request.getParameter("JOBCODES").toString();   //作业订单编码

		msg.setData("JobOrderPair", lastbottompairservice.getJobOrderPairLists(JOBCODES));

		msg.setData("JobOrderSoles", lastbottompairservice.getBatchnoSoleLists(JOBCODES));

		return msg;
	}

	//根据传过来的RFID号带出楦头、大底的基础信息
	@ResponseBody
	@RequestMapping(value = "getShoeResult", method = RequestMethod.POST)
	public Message getShoeResult(HttpServletRequest request) {
		Message msg = new Message();

		String batch_no = request.getParameter("batch_no").toString();   //扫描的RFID信息

		String batch_nos = batch_no.replaceAll(" ", "");

		msg.setData("ShoeLists", lastbottompairservice.getBatchnorLists(batch_nos));

		return msg;
	}

	//处理配对结果
	@ResponseBody
	@RequestMapping(value = "SaveLstBottimPariLists", method = RequestMethod.POST)
	public Message OutConfirmResult(HttpServletRequest request) {
		Message msg = new Message();
		try {

			String leftShoeID = request.getParameter("leftShoeID").toString();   //左脚楦头RFID
			String rightShoeID = request.getParameter("rightShoeID").toString();   //右脚楦头RFID
			String leftsoleID = request.getParameter("leftsoleID").toString();   //左脚大底RFID
			String rightsoleID = request.getParameter("rightsoleID").toString();   //右脚大底RFID
			String StatusUp = request.getParameter("StatusUp").toString();   //判读当前提交为回流配对还是车间生产配对
			//String rightsoleID = request.getParameter("leftsoleID").toString();   //右脚大底RFID(当前不区分大底左右脚)
			String planId = request.getParameter("planId").toString();   //作业工单ID

			if(StatusUp.equals("HL")){
				List<Map<String, Object>> Lists = lastbottompairservice.selectPlanCode(planId);
				String JobCode = Lists.get(0).get("JOBCODE").toString();  //工单编码
				String warehouseID = "1E0A69B1B7BB4EECB232D6989684C4D7";
				String locatorID = null;
				User user = UserUtil.getUser();


				//左脚楦头信息
				List<Map<String, Object>> LeftList = lastbottompairservice.getBatchnorLists(leftShoeID);
				String LetfbatchId = LeftList.get(0).get("BATCHID").toString();  //BATCH_ID
				String LeftDWID = LeftList.get(0).get("DWID").toString();  //单位ID
				lastbottompairservice.inStorageInOut(warehouseID,LetfbatchId, locatorID, -1, LeftDWID, "", user, leftShoeID);


				//右脚楦头信息
				List<Map<String, Object>> RightList = lastbottompairservice.getBatchnorLists(rightShoeID);
				String RightbatchId = RightList.get(0).get("BATCHID").toString();  //BATCH_ID
				String RightDWID = RightList.get(0).get("DWID").toString();  //单位ID
				lastbottompairservice.inStorageInOut(warehouseID,RightbatchId, locatorID, -1, RightDWID, "", user, rightShoeID);
			}


			lastbottompairservice.updateJobPlanOrder(leftShoeID, rightShoeID, leftsoleID, rightsoleID, planId);

			lastbottompairservice.updateStorageBatchItem(leftShoeID);
			lastbottompairservice.updateStorageBatchItem(rightShoeID);
			lastbottompairservice.updateStorageBatchItem(leftsoleID);
			lastbottompairservice.updateStorageBatchItem(rightsoleID);
		} catch (Exception e) {
			msg.setData("results", "false");
			e.printStackTrace();
		}

		return msg;
	}
}
	
	
	
	
